#include <iostream>
#include "0D/F1.h"
#include "afn0d.h"

bool AFN0D_Handler::handle(Controller& controller)
{
	UserBuffer &rb = controller.recvbuf;
	UserBuffer &sb = controller.sendbuf;

	sb = rb;
	for(int i = 0; i < rb.getNum(); i++)
	{
		switch( rb.getFn(i) )
		{
			case 1: handleF01(controller, i); break;
			case 2: handleF02(controller, i); break;
			case 3: handleF03(controller, i); break;
			case 4: handleF04(controller, i); break;
			case 5: handleF05(controller, i); break;
			case 6: handleF06(controller, i); break;
			case 7: handleF07(controller, i); break;
			case 8: handleF08(controller, i); break;
		}
	}

	sb.setPrm(MT_PRM_PASIVE);
	sb.setDir(MT_DIR_S2M);
	sb.setCon(false);

	sb.clear();
	if( sb.packup() )
	{
		cout << __func__ << "P:" << sb.toStringU() << endl;
		if( controller.send_n(sb.data, sb.length) == sb.length )
		{
			cout << __func__ << "X:" << sb.toStringF() << endl;
			cout << __func__ << "T:" << sb.toStringU() << endl;
			sb.init();
			sb.unpack();
			cout << __func__ << "X:" << sb.toStringF() << endl;
			cout << __func__ << "T:" << sb.toStringU() << endl;
			return true;
		}
	}



	return false;
}
bool AFN0D_Handler::handleF01(Controller& controller, int index)
{
	Afn0df1 f1;
	uMtApp  &app =  controller.recvbuf.getApp(index);

	printf("F%d  P%d  Td_d=%s\n",
			controller.recvbuf.getFn(index), 
			controller.recvbuf.getPn(index), 
			f1.getPnString(app).data());

	return f1.handle(controller, index);
}
bool AFN0D_Handler::handleF02(Controller& controller, int index)
{
	return false;
}
bool AFN0D_Handler::handleF03(Controller& controller, int index)
{
	return false;
}
bool AFN0D_Handler::handleF04(Controller& controller, int index)
{
	return false;
}
bool AFN0D_Handler::handleF05(Controller& controller, int index)
{
	return false;
}
bool AFN0D_Handler::handleF06(Controller& controller, int index)
{
	return false;
}
bool AFN0D_Handler::handleF07(Controller& controller, int index)
{
	return false;
}
bool AFN0D_Handler::handleF08(Controller& controller, int index)
{
	return false;
}

